$(function() {
	var on = true;
	$(".headBtn").click(function() {
		if(on) {
			$(".topbar-nav").css({
				"height": "363px"
			});
		} else {
			$(".topbar-nav").css({
				"height": "0"
			});
		}
		on = !on;
	});
	
	//轮播1
	var index = 0;
	var length = $(".indexes.bfocus li").length;
	var ulWidth = length * 100 + "%";
	$(".focusShow.bfocus").css("width", ulWidth);
	$(".focusShow.bfocus li").css("width", 100 / length + "%");

	//索引切换
	$(".indexes.bfocus li").click(function() {
		index = $(this).index();
		init();
	});

	//轮播图向前翻
	$(".prev.bfocus").click(function() {
		index--;
		if(index < 0) {
			index = length - 1;
		}
		init();
	});

	//轮播图向后翻
	$(".next.bfocus").click(function() {
		add();
	});

	//focus移入移出停止/启动定时器
	$(".focus.bfocus").hover(function() {
		clearInterval(timer);
	}, function() {
		autoplay();
	});

	//定时器
	var timer = 0;

	function autoplay() {
		timer = setInterval(function() {
			add();
		}, 6000);
	}

	//启动定时器
	autoplay();

	//封装函数
	function add() {
		index++;
		if(index > length - 1) {
			index = 0;
		}
		init();
	}

	function init() {
		$(".focusShow.bfocus").css("transform", "translateX(-" + (100 / length) * index + "%)");
		$(".indexes.bfocus li").eq(index).addClass("indexes-active").siblings().removeClass("indexes-active");
	}

	//轮播2
	var index2 = 0;
	var indLi = 0;
	var ulsfocus = 0;
	var sfocus = 0;
	var length2 = 0;
	pageInit();

	$(window).resize(function() {
		pageInit();
	});

	//向前翻
	$(".prev.sfocus").click(function() {
		index2--;
		if(index2 < 0) {
			index2 = indLi - 1;
		}
		init2();
	});

	//向后翻
	$(".next.sfocus").click(function() {
		index2++;
		if(index2 > indLi - 1) {
			index2 = 0;
		}
		init2();
	});

	//轮播2页面初始化函数封装
	function pageInit() {
		//初始化轮播2ul的宽度,根据li的数量
		length2 = $(".focusShow.sfocus li").length;
		$(".focusShow.sfocus").css({
			"width": 155 * length2
		});

		//初始化索引的数量，根据ul的宽度
		$(".indexes.sfocus").empty();
		ulsfocus = $(".focusShow.sfocus").width();
		sfocus = $(".focus.sfocus").width();
		indLi = Math.ceil(ulsfocus / sfocus);
		for(var i = 0; i < indLi; i++) {
			$(".indexes.sfocus").append("<li></li>");
		}
		init2();

		//索引切换
		$(".indexes.sfocus li").click(function() {
			index2 = $(this).index();
			init2();
		});
	}

	function init2() {
		$(".focusShow.sfocus").css("transform", "translateX(-" + ((155 * length2) / indLi) * index2 + "px)");
		$(".indexes.sfocus li").removeClass("indexes-active").eq(index2).addClass("indexes-active");
	}
});